package gLib::gDebug;

## ----------------------------------------------------
## Title :			Generic Debug Class
## Description :	Provide generic functions used to debug and maintain perl script
## Date :			2013-07-11
## Version :		0.1
## Author :			@TiTom
## ----------------------------------------------------

## ----------------------------------------------------
## Todo List
## ----------------------------------------------------
## - Insert Perl Document comments
## ----------------------------------------------------

=head1 NAME

gDebug Module

=head1 DESCRIPTION

Implement functions to provide log and debug informations in your perl script

=head1 Author

@TiTom73 (dev@inetsix.net)

=head1 SYNOPSYS

Usage is :
	
	use gLib::gDebug;
	
	my $debug = new gLib::gDebug;
	$debug->file("/path/to/log/file.log");
	printf( "Log file is set to : %s\n",$debug->file);
	# Print log in your console :
	$debug->log("Test 1");
	
	# Send log to file :
	$debug->syslog("I wanna be Perl Developper !");

Generated Log file is like below :
	2013-07-04 - 18:18:57 - function=main - msg=I wanna be Perl Developper !
	2013-07-04 - 18:18:57 - function=main::test - msg=It is always better than to be PHP dev ;)

=head1 DEPENDENCIES

This module is based on :
- strict
- warnings
- POSIX
=cut

########################################################
#	Requirements
########################################################
use strict;
use warnings;
use POSIX qw/strftime/;

########################################################
#	Code Version
########################################################
=head1 VERSION

Verison = 0.1
=cut 
our $VERSION = 0.1;

########################################################
#	Constants
########################################################

use constant TRUE=> 1;
use constant FALSE=> 0;

########################################################
#	FUNCTIONS
########################################################
=head1 METHODS
=cut
## Package to provide some usefull functions
## Class Constructor
sub new {
	my ($class) = @_;

	my $self = {
		_DEBUG		=> TRUE,
		_SYSLOG		=> TRUE,
		_LOGFILE	=> "/home/tom/nas/02-Scripts/glibrary73/debug.log",
	};

	bless	$self,$class;
	return	$self;
}

## Create new log entry
=head2 log

Print log to your console only if DEBUG is set to TRUE
Log is construct with date/time / function where log has been set / message

	2013-07-04 - 18:18:57 - function=main::test - msg=blabla

=cut
sub log {
	my ($self,$msg) = @_;
	my $func = undef;
	
	## Check if function has been called for another subroutine
	if( defined((caller(1))[3]) ) { $func = (caller(1))[3] }
	## Otherwise, used parent class
	else { $func = (caller(0)) }

	## Display log only if DEBUG is enable
	if($self->{_DEBUG} == TRUE) {
		printf("%s",$self->_construct_log($func,$msg));
		return TRUE;
	}
	else { return FALSE }
}

## Function to construct log entry
## Private subroutine
=head2 _construct_log

Format log message before display or write it to your logfile.

=cut
sub _construct_log {
	my ($self,$func,$msg) = @_;
	## Provide date and time for any log message
	my $log = strftime("%Y-%m-%d - %H:%M:%S - ", localtime)."function=$func - msg=$msg\n";
	return $log;
}

## Function to write log to _LOGFILE
=head2 syslog

Print log to your syslog file only if DEBUG is set to TRUE
Log is construct with date/time / function where log has been set / message

	2013-07-04 - 18:18:57 - function=main::test - msg=blabla

=cut
sub syslog {
	my ($self,$msg) = @_;
	my $func = undef;
	
	## Check if function has been called for another subroutine
	if( defined((caller(1))[3]) ) { $func = (caller(1))[3] }
	## Otherwise, used parent class
	else { $func = (caller(0)) }

	## Display log only if DEBUG is enable
	if($self->{_SYSLOG} == TRUE) {
		## Append function to write log to our file _LOGFILE	
		open FILE, ">>", $self->{_LOGFILE} or die $!;
		print FILE $self->_construct_log($func,$msg) ;
		close FILE;
	}
	return TRUE;
}

## Function to manage DEBUG Value
=head2 debug

Set DEBUG value to enable or disable it.
If there is no value, function return STATUS

=cut
sub debug {
	my ( $self, $debug ) = @_;
	$self->{_DEBUG} = $debug if defined($debug);
	return $self->{_DEBUG};
}

## Function to manage SYSLOG Value
=head2 file

Set _SYSLOG value to enable or disable it.
If there is no value, function return _SYSLOG value

=cut
sub file {
	my ( $self, $syslog ) = @_;
	$self->{_LOGFILE} = $syslog if defined($syslog);
	return $self->{_LOGFILE};
}
1;
